<?php
class Model_Blockmapping extends Model{

    protected static $_instance =  null;
	/**
     * @return Model_Blockmapping
     */
	static public function getInstance(){
        if(is_null(self::$_instance))	
           self::$_instance = new self('blockmapping');     
        return self::$_instance;   
    }
    
    
 	/**
     * Clear block map for page by page ID
     * @param integer $pageId
     * @return void
     */
    public function clearMap($pageId){
        $this->_db->delete($this->table(),' `page_id` = ' . intval($pageId));
    }
    /**
     * Add block map for page
     * @param integer $pageId
     * @param string $code
     * @param array $blockIds
     */
    public function addBlocks($pageId , $code , array $blockIds){
        if(empty($blockIds))
            return true;
            
        $order = 0;
        foreach ($blockIds as $id){
            $blockmapItem = new Db_Object('blockmapping');
            $blockmapItem->set('block_id' , $id);
            $blockmapItem->set('page_id' , $pageId);
            $blockmapItem->set('place' , $code);
            $blockmapItem->set('order_no' , $order);
            $blockmapItem->save(false);
            $order++;
        }    
        return true;    
    }
}